package com.imooc.springboot.dubbo.nilinili.provider.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.imooc.springboot.dubbo.nilinili.provider.mapper.FanMapper;
import com.imooc.springboot.dubbo.nilinili.provider.utils.JwtToUserId;
import com.imooc.springboot.dubbo.nilinili.res.CodeEnum;
import com.imooc.springboot.dubbo.nilinili.res.ResultFactory;
import com.imooc.springboot.dubbo.nilinili.service.FanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = FanService.class)
public class FanServiceImpl implements FanService {

    @Autowired
    private FanMapper fanMapper;

    @Override
    public String getFanList(String token) {
        String userId = JwtToUserId.Jwt2UserId(token);
        return ResultFactory.createResult(CodeEnum.SUCCESS,"查询成功",fanMapper.getFanList(userId));
    }

    @Override
    public String getFollowedList(String token) {
        String userId = JwtToUserId.Jwt2UserId(token);
        return ResultFactory.createResult(CodeEnum.SUCCESS,"查询成功",fanMapper.getFollowedList(userId));
    }

    /* 判断是否关注了当前用户 */
    @Override
    public String getIsFollowed(String token, String followedId) {
        String userId = JwtToUserId.Jwt2UserId(token);
        //  直接进表中查询是否存在数据即可
        int isFollowed = fanMapper.getIsFollowed(userId, followedId);
        if (isFollowed == 0) {
            return ResultFactory.createResult(CodeEnum.SUCCESS,"未关注",0);
        } else {
            return ResultFactory.createResult(CodeEnum.SUCCESS,"已关注",1);
        }
    }

    /* 关注 */
    @Override
    @Transactional
    public String followAction(String token, String followedId) {
        String userId = JwtToUserId.Jwt2UserId(token);
        if (userId.equals(followedId)) {
            return ResultFactory.createResult(CodeEnum.FAIL,"不能关注自己");
        }
        //  插入数据，如果已经关注，禁止插入数据
        synchronized (this){
            int isFollowed = fanMapper.getIsFollowed(userId, followedId);
            if (isFollowed == 0) {
                // 插入数据
                fanMapper.followAction(userId, followedId);
                //  计算出当前用户粉丝数量，更新值user表中
                fanMapper.setNewFanCount(followedId, String.valueOf(fanMapper.getFanList(followedId).size()));
                fanMapper.setNewFollowCount(userId, String.valueOf(fanMapper.getFollowedList(userId).size()));
                return ResultFactory.createResult(CodeEnum.SUCCESS,"关注成功");
            } else {
                return ResultFactory.createResult(CodeEnum.SUCCESS,"已关注");
            }
        }
    }

    /* 取消关注 */
    @Override
    @Transactional
    public String cancleFollowAction(String token, String followedId) {
        String userId = JwtToUserId.Jwt2UserId(token);
        if (userId.equals(followedId)) {
            return ResultFactory.createResult(CodeEnum.FAIL,"不能取消关注自己");
        }
        synchronized (this){
            int isFollowed = fanMapper.getIsFollowed(userId, followedId);
            if (isFollowed == 0) {
                return ResultFactory.createResult(CodeEnum.SUCCESS,"未关注");
            } else {
                // 删除数据
                fanMapper.cancleFollowAction(userId, followedId);
                //  计算出当前用户粉丝数量，更新值user表中
                fanMapper.setNewFanCount(followedId, String.valueOf(fanMapper.getFanList(followedId).size()));
                fanMapper.setNewFollowCount(userId, String.valueOf(fanMapper.getFollowedList(userId).size()));
                return ResultFactory.createResult(CodeEnum.SUCCESS,"取消关注成功");
            }
        }
    }
}
